package com.gr.springboot_shiro.config;

import com.gr.springboot_shiro.cache.RedisCacheManager;
import com.gr.springboot_shiro.realm.MyRealm;
import com.gr.springboot_shiro.utils.RedisConnection;
import org.apache.shiro.authc.credential.HashedCredentialsMatcher;
import org.apache.shiro.realm.Realm;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;

@Configuration
public class ShiroConfig {

    /**
     * 创建shiroFilter拦截所有请求
     */
    @Bean("shiroFilterFactoryBean")
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultWebSecurityManager defaultWebSecurityManager) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultWebSecurityManager);

        HashMap<String, String> map = new HashMap<String, String>();

        //authc 请求这个资源需要认证和授权
        map.put("/reg.jsp", "anon");
        //map.put("/user/login", "anon");
        //map.put("/user/register", "anon");
        //map.put("/user/getImage", "anon");
        map.put("/user/*", "anon");
        map.put("/**", "authc");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);

        shiroFilterFactoryBean.setLoginUrl("/login.jsp");
        return shiroFilterFactoryBean;
    }

    /**
     * 创建安全管理器
     *
     * @param realm
     * @return
     */
    @Bean
    public DefaultWebSecurityManager getDefaultWebSecurityManager(@Qualifier("myRealm") Realm realm) {
        DefaultWebSecurityManager defaultWebSecurityManager = new DefaultWebSecurityManager();
        defaultWebSecurityManager.setRealm(realm);
        return defaultWebSecurityManager;
    }

    @Bean("myRealm")
    public Realm getRealm() {
        MyRealm myRealm = new MyRealm();
        //修改凭证校验器
        HashedCredentialsMatcher hashedCredentialsMatcher = new HashedCredentialsMatcher();
        //设置加密算法为MD5
        hashedCredentialsMatcher.setHashAlgorithmName("MD5");
        //设置散列次数
        hashedCredentialsMatcher.setHashIterations(1024);
        myRealm.setCredentialsMatcher(hashedCredentialsMatcher);
        //判断redis是否连接
        int res = RedisConnection.getRedisIsOk("127.0.0.1", 6379);
        if (res == 0) {
            System.out.println("redis服务已开启！" + res);

            //开启缓存
            myRealm.setCachingEnabled(true);
            //开启认证缓存
            myRealm.setAuthenticationCachingEnabled(true);
            myRealm.setAuthenticationCacheName("AuthenticationCache");
            //开启授权缓存
            myRealm.setAuthorizationCachingEnabled(true);
            myRealm.setAuthorizationCacheName("AuthorizationCache");
            //myRealm.setCacheManager(new EhCacheManager());
            myRealm.setCacheManager(new RedisCacheManager());
        } else {
            System.out.println("redis服务未开启！" + res);
        }

        return myRealm;
    }


}
